<?php
require_once("serial.php");

$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0");

// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");

if(array_key_exists('data', $_GET)) {
  
  // Then we need to open it
  $serial->deviceOpen();

  // To write into
  $serial->sendMessage($_GET['data']);
  
  $read = $serial->readPort(); 

  //there is a bug when reading data: it seems the firmware only reads one byte
  if(strlen($read) > 0) {
    $serial->sendMessage('a');
  }
  
  $serial->deviceClose();

  echo('data sent: \'' . $_GET['data'] . '\'; ');
  echo('data read: \'' . $read . '\'');
}
else {
  echo('no data to send');
}

?>
